import {questionData} from '@/static/js/monitor/tcm/qusetionnaireDetail.js';
import { getUrlParam} from '@/static/js/filter.js';
export default {
	data () {
		return {
			questionData:JSON.parse(JSON.stringify(questionData)),
			currentIndex: 0,
			nowType:'平和质',
			questionArr:[], //tab标题
			detailsType: '',
			sex: '',
			current: '', //从体质辨识点击返回回来的状态
			showModal: false,
			showContent: '',
		}
	},
	onLoad() {
		// 从缓存中获取性别
		this.sex = JSON.parse(localStorage.getItem('userInfo')).sex;
		this.sex == 2 ? this.questionData[5].data.splice(6,1): this.questionData[5].data.splice(5,1);
		this.questionData.map((a) => {
			a.data.map((b) => {
				b.options.map((c) => {
					c.checked = false;
					c.checked2 = false;
				})
			})
		})
		this.questionArr = this.questionData.filter(a=>{return a.name == this.nowType})[0].data;
			// 获取tab栏内容
			this.namePage = this.questionData.map((a) => {
				return a.name
			})
			
			// 获取详情
			this.id = getUrlParam("id");
			this.current = getUrlParam('current');
			if(this.current) {
				this.questionArr = this.questionData[this.currentIndex].data;
				this.currentIndex = this.current;
			}
			if(getUrlParam("id")){
				this.$api.questionnaireInfoId(this.id).then((res) => {
					this.detailsType = res.data.result && res.data.result.appConstitutionEntityVo.status;
					this.questionData.map((a,index)=>{
						res.data.result && res.data.result[a.name].map((b,bindex)=>{
							a.data[bindex] && a.data[bindex].options.map((c)=>{
								c.id = b.id
								if(c.userOption == b.userOption){
									c.checked = true
								}
								if(b.healthOption == c.userOption){
									c.checked2 = true
								}
							})
						})
					})
				})
				
			} else {
				// 从详情页点击回来查看选中的详情
				if(this.current) {
					const questionHistory = localStorage.getItem('questionData') && JSON.parse(localStorage.getItem('questionData'));
					this.questionData = questionHistory;
					this.questionArr = this.questionData[this.currentIndex].data;
				}
			}
	},
	mounted(){
		if(!this.current) {
			this.questionArr = this.questionData[0].data;			
		} else {
			this.questionArr = this.questionData[this.currentIndex].data;
		}
	},
	methods: {
		// tab切换
		switchClick(name,index){
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
			});
			this.nowType = name;
			this.currentIndex = index;
			this.questionArr = this.questionData.filter(a=>{return a.name == name})[0].data;
			// this.$api.toBbtainTCM(this.id,{userId:this.id}).then((res) => {
			// 	questionData.map((a,index)=>{
			// 		res.data.Result[a.name].map((b,bindex)=>{
			// 			a.data[bindex] && a.data[bindex].options.map((c)=>{
			// 				c.id = b.id
			// 				if(c.userOption == b.userOption){
			// 					c.checked = true
			// 					c.checked2 = true
			// 				}
			// 				if(b.healthOption == c.userOption){
			// 					c.checked2 = true
			// 				}
			// 			})
			// 		})
			// 	})
			// })
		},
		// 判定跳转
		determine() {
			if(this.id) {
				uni.navigateTo({
					url:'/pages/monitor/tcm/syndrome?id='+this.id+'&status='+this.detailsType
				})
				return 
			}
			this.filterResult = this.questionData.map((a)=>{
				const bresult = a.data.filter((b)=>{
					b.nullchecked = false
					const cresult = b.options.filter((c)=>{
						return c.checked
					})
					if(cresult.length <= 0){
						b.nullchecked = true
					}
					return b.nullchecked;
				})
				a.bresult = bresult
				return a
			})
			this.i = 0;
			this.showToast(this.filterResult);
		},
		showToast(filterResult){
			if(filterResult.length > 0){
				if(filterResult[this.i]){
					if(filterResult[this.i].bresult.length > 0){
						this.showModal = true,
						this.showContent = '请选择'+filterResult[this.i].name+'底下的'+filterResult[this.i].bresult[0].title
						// uni.showToast({title:'请选择'+filterResult[this.i].name+'底下的'+filterResult[this.i].bresult[0].title,icon:'none'})
					}else{
						this.i++
						this.showToast(this.filterResult);
					}
				}else{
					uni.navigateTo({
						url:'/pages/monitor/tcm/syndrome'
					})
				}
			}
		},
		Modalconfirm() {
			this.showModal = false
		},
			// 上一页
		prevPage(){
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
			});
			if(this.currentIndex == 0){
				uni.showToast({title:'这已经是第一页了~',icon:'none'})
				return;   
			}
			this.currentIndex--;
			this.questionArr = this.questionData[this.currentIndex].data;
		},
		// 下一页
		nextPage() {
			const _this = this;     
			uni.pageScrollTo({
				 scrollTop: 0,
				 duration: 0
			});
			if(_this.currentIndex == 8) {
				_this.determine();
				return; 
			}
			_this.currentIndex++;          
			_this.questionArr = _this.questionData[_this.currentIndex].data
		},
		// 选答案
		selectedTitle(aitem,item) {
			if(this.id && this.detailsType == 2){
				return                    
			} else {
				item.options.map((a)=>{a.checked = false})
				aitem.checked = true
				localStorage.setItem('questionData',JSON.stringify(this.questionData))
			}               
		}
	}
}